package com.neusoft.neumooc.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.VO.OrderVO;

@Repository
public class OrderDaoImpl extends BaseDAO implements OrderDao {

	/**
	 * @Fields INSERT_ORDER_SQL : TODO 添加订单
	 */
	private final static String INSERT_ORDER_SQL = "INSERT INTO final_order(id,userid,orderid,totalprice,pay_style,pay_status,note,order_status,time,cartdata) VALUES(:id,:userId,:orderId,:totalPrice,:payStyle,:payStatus,:note,:orderStatus,NOW(),:cartData)";

	@Override
	public int addOrder(OrderVO orderVO) throws DAOException {
		return this.update(INSERT_ORDER_SQL, orderVO);
	}

	/**
	 * @Fields QUERY_ORDER_NUM : TODO 查询订单数量
	 */
	private final static String QUERY_ORDER_NUM = "SELECT count(1) FROM final_order where userid=?";

	@Override
	public int getOrderNum(String userId) throws DAOException {
		Object[] args = { userId };
		return this.queryInteger(QUERY_ORDER_NUM, args);
	}

	private final static String GET_ORDER = "SELECT id,userid as userId,orderid as orderId,totalprice as totalPrice,pay_style as payStyle,pay_status as payStatus,order_status as orderStatus,note,cartdata as cartData FROM final_order where userid=?";

	@Override
	public List<OrderVO> getOrderList(String userId, int pageIndex, int pageSize)
			throws DAOException {
		Object[] args = { userId };
		return this.getPageData(GET_ORDER, OrderVO.class, pageIndex, pageSize,
				args);
	}
}
